<?php
// 数据库配置
session_start();

if (!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}
include 'config.php';
// 获取表的数量，根据特定条件
function get_table_count_by_condition($conn, $table_name, $condition = '') {
    $sql = "SELECT COUNT(*) as count FROM $table_name" . ($condition ? " WHERE $condition" : "");
    $result = $conn->query($sql);

    if (!$result) {
        // 输出错误信息
        die("查询失败: " . $conn->error);
    }

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        return $row['count'];
    } else {
        return 0;
    }
}

// 获取user表的数量
$user_count = get_table_count_by_condition($conn, 'sq');

// 获取kami表的总数
$kami_count_total = get_table_count_by_condition($conn, 'kami');

// 获取订单数
$order_count = get_table_count_by_condition($conn, 'orders');

//订单支付数
$order_count1 = get_table_count_by_condition($conn, 'orders', "type=1");

//当前版本


//订单未支付数
$order_count2 = get_table_count_by_condition($conn, 'orders', "type=0");

// 获取kami表中zt为0的数量
$kami_count_zt0 = get_table_count_by_condition($conn, 'kami', "zt=0");

// 获取kami表中zt为1的数量
$kami_count_zt1 = get_table_count_by_condition($conn, 'kami', "zt=1");

// 将数量转换为JSON数组
$json_data = array(
    'user' => $user_count,
    'blacklist' => $black_count,
    'kami' => array(
        'total' => $kami_count_total,
        'zt0' => $kami_count_zt0,
        'zt1' => $kami_count_zt1
    ),
      'orders' => array(
        'total' => $order_count,
        '1' => $order_count1,
        '0' => $order_count2
    )
    
);

// 输出JSON数组
header('Content-Type: application/json');
echo json_encode($json_data);

// 关闭数据库连接
$conn->close();
?>